<?php
namespace App\Api;

use PhalApi\Api;
use App\Domain\Im as Domain_Im;
use App\Domain\Vip as Domain_Vip;
/**
 * 私信
 */

class Im extends Api {

	public function getRules() {
        return array(
            'getMultiInfo' => array(
                'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'uids' => array('name' => 'uids', 'type' => 'string', 'desc' => '用户ID，多个以逗号分割'),
			),
            
            'getSysNotice' => array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'p' => array('name' => 'p', 'type' => 'string', 'desc' => '页码'),
			),
            
            'check' => array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'touid' => array('name' => 'touid', 'type' => 'int', 'desc' => '对方ID'),
			),
            
            'buyIm' => array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
			),

			'checkIMMsg'=>array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'content' => array('name' => 'content', 'type' => 'string', 'desc' => '用户发送的私信内容'),
			),

			'addImNum'=>array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token')
			)
        );
	}
	
	/**
	 * 获取多用户信息 
	 * @desc 用于获取多用户信息
	 * @return int code 操作码，0表示成功
	 * @return array info 列表
	 * @return string info[].isauth 是否主播认证，0否，1是
	 * @return string info[].isuser_auth 是否实名认证，0否，1是
	 * @return string info[].u2t 是否关注对方，0否1是
	 * @return string info[].isvip 是否VIP，0否1是
	 * @return string msg 提示信息
	 */
	public function getMultiInfo() {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
		
        $uid=\App\checkNull($this->uid);
        $uids=\App\checkNull($this->uids);
        if($uids==''){
            //$rs['code'] = 1001;
			//$rs['msg'] = \PhalApi\T('信息错误');
			return $rs;
        }
		$uids_a=explode(",",$uids);
        
        $domain_vip = new Domain_Vip();
        
        $configpri=\App\getConfigPri();
		$sdkappid=$configpri['im_sdkappid'];
		$identifier=$configpri['im_admin'];
        
        $list=[];
		foreach ($uids_a as $k=>$userId) {
            if($userId==$identifier){
                $list[]=[
                    "id"=>$identifier,
                    "user_nickname"=>\PhalApi\T("官方通知"),
                    "avatar"=> \App\get_upload_path("/static/app/sysnotice.png"),
                    "avatar_thumb"=>\App\get_upload_path("/static/app/sysnotice.png"),
                    "sex"=>"0",
                    "signature"=>"",
                    "birthday"=> "0",
                    "level"=> "1",
                    "level_anchor"=> "1",
                    "isauth"=>"0",
                    "isauthor_auth"=>"0",
                    "u2t"=>"0",
                    "isvip"=>"0",
                    "isblack"=>"0",
                ];
            }else if($userId > 0){
				$userinfo= \App\getUserInfo($userId);
				$userinfo['isauth']= \App\isAuthorAuth($userId);
				$userinfo['isuser_auth']= \App\isAuth($userId);
                $userinfo['u2t']= \App\isAttention($uid,$userId);
                $userinfo['isblack']= \App\isBlack($uid,$userId);
                
                /* VIP */
                $vipinfo=$domain_vip->myVip($userId);
                $userinfo['isvip']=$vipinfo['isvip'];
            
                $list[]=$userinfo;
			}
		}

        $rs['info']=$list;
		return $rs;
	}

	/**
	 * 系统通知 
	 * @desc 用于获取系统通知
	 * @return int code 操作码，0表示成功
	 * @return array info 列表
	 * @return string info[].content 信息内容
	 * @return string info[].addtime 时间
	 * @return string msg 提示信息
	 */
	public function getSysNotice() {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
		
        $uid=\App\checkNull($this->uid);
        $token=\App\checkNull($this->token);
        $p=\App\checkNull($this->p);
        $checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}

        
        $domain = new Domain_Im();
		$list = $domain->getSysNotice($uid,$p);

        $rs['info']=$list;
		return $rs;
	}

	/**
	 * 私信检测 
	 * @desc 用于私信检测
	 * @return int code 操作码，0表示成功
	 * @return array info 
	 * @return string msg 提示信息
	 */
	public function check() {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
		
        $uid=\App\checkNull($this->uid);
        $token=\App\checkNull($this->token);
        $touid=\App\checkNull($this->touid);

        
        $checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}

		$where=[
			'user_type'=>2,
			'id'=>$touid,
		];
		$to_userinfo=\App\checkUser($where);
		
		if(!$to_userinfo){
			$rs['code'] = 1002;
			$rs['msg'] = \PhalApi\T('用户不存在');
			return $rs;
		}
		
		$iscancellation=\App\checkIsDestroyByLogin($touid,'0');
		if($iscancellation){
			$rs['code']=1001;
			$rs['msg']=\PhalApi\T('对方账号已被注销,不能使用');
			return $rs;	
		}
        
        $domain = new Domain_Im();
		$result = $domain->check($uid,$touid);

		return $result;
	}

	/**
	 * 私信购买 
	 * @desc 用于私信购买
	 * @return int code 操作码，0表示成功
	 * @return array info 列表
	 * @return string msg 提示信息
	 */
	public function buyIm() {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
		
        $uid=\App\checkNull($this->uid);
        $token=\App\checkNull($this->token);
        
        $checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}
        
        $domain = new Domain_Im();
		$result = $domain->buyIm($uid);

		return $result;
	}

    /**
     * 私信聊天敏感词替换
     * @desc 私信聊天时检测敏感词替换
     * @return int code 状态码,0表示成功
     * @return string info 返回信息
     * @return int code 状态码,0表示成功
     */
    public function checkIMMsg(){

    	$rs = array('code' => 0, 'msg' => '', 'info' => array());
		
        $uid=\App\checkNull($this->uid);
        $token=\App\checkNull($this->token);
        $content=\App\checkNull($this->content);
        
        $checkToken=\App\checkToken($uid,$token);
        
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}

		if($content==''){
			$rs['code'] = 1001;
			$rs['msg'] = \PhalApi\T('请输入聊天内容');
			return $rs;
		}

		$content=\App\changeSensitiveWords($content);
		$rs['info'][0]['str']=$content;
		return $rs;

    }
    
    /**
     * 私信聊天发送后用于每日任务检测次数
     * @desc 私信聊天发送后用于每日任务检测次数
     * @return int code 状态码,0表示成功
     * @return string info 返回信息
     * @return int code 状态码,0表示成功
     */
    public function addImNum(){
    	$rs = array('code' => 0, 'msg' => '', 'info' => array());
		
        $uid=\App\checkNull($this->uid);
        $token=\App\checkNull($this->token);

        $checkToken=\App\checkToken($uid,$token);
        
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}

		//每日任务
        $dailytask_data=[
			'type'=>'send_privatemsg',
			'nums'=>1
		];

		\App\dailyTasks($uid,$dailytask_data);

		return $rs;
    }	
    
}
